package com.yuexun.dhdoctor.module.commun.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupBean;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/17 0017.
 */

public class DepartAndGroup_Group_Adapter extends BaseExpandableListAdapter {

    private Context context;
    private List<GroupGroupBean> groupGroupBeen = new ArrayList<>();

    private Handler handler;
    private ArrayList selectValues;
    //0：不能选择自己  1：能选择自己
    private int isSelectMe;

    public DepartAndGroup_Group_Adapter(Context context, List<GroupGroupBean> groupGroupBeen,
                                        ArrayList selectValues, Handler handler, int isSelectMe) {
        this.context = context;
        this.groupGroupBeen = groupGroupBeen;
        this.selectValues = selectValues;
        this.handler = handler;
        this.isSelectMe = isSelectMe;
    }

    public void setGroupGroupBeen(List<GroupGroupBean> groupGroupBeen) {
        this.groupGroupBeen = groupGroupBeen;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return null == groupGroupBeen ? 0 : groupGroupBeen.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return groupGroupBeen.get(i).getDatas().size();
    }

    @Override
    public Object getGroup(int i) {
        return groupGroupBeen.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return groupGroupBeen.get(i).getDatas().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewGroupHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_parent, null);
            holder = new ViewGroupHolder();
            holder.groupName = (TextView) view.findViewById(R.id.item_group_name);
            view.setTag(holder);
        } else {
            holder = (ViewGroupHolder) view.getTag();
        }

        holder.groupName.setText(groupGroupBeen.get(i).getGroup_name());
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        ViewChildHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_group, null);
            holder = new ViewChildHolder();
            holder.group_name = (TextView) view.findViewById(R.id.item_group_name);
            holder.group_count = (TextView) view.findViewById(R.id.item_group_count);
            holder.checkBox = (CheckBox) view.findViewById(R.id.add_checkbox);
            view.setTag(holder);
        } else {
            holder = (ViewChildHolder) view.getTag();
        }

        holder.group_name.setText(groupGroupBeen.get(i).getDatas().get(i1).getTitle());
        holder.group_count.setText(groupGroupBeen.get(i).getDatas().get(i1).getToId().size() + "人");
        holder.checkBox.setVisibility(View.VISIBLE);

        holder.checkBox.setChecked(false);

        for (int j = 0; j < selectValues.size(); j++) {
            if (selectValues.get(j) instanceof GroupGroupData) {
                if (((GroupGroupData) selectValues.get(j)).getGroupId().equals(groupGroupBeen.get(i).getDatas().get(i1).getGroupId())) {
                    holder.checkBox.setChecked(true);
                }
            }
        }

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!((CheckBox) view).isChecked()) {
                    selectValues.remove(groupGroupBeen.get(i).getDatas().get(i1));
//                    checkBox.setChecked(false);
//                    notifyDataSetChanged();
                    Message msg = new Message();
                    msg.what = 10003;
                    handler.sendMessage(msg);
                } else {
                    selectValues.clear();
                    selectValues.add(groupGroupBeen.get(i).getDatas().get(i1));
//                    checkBox.setChecked(true);
                    Message msg = new Message();
                    msg.what = 10003;
                    handler.sendMessage(msg);
                    notifyDataSetChanged();
                }
            }
        });

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class ViewGroupHolder {
        TextView groupName;
    }

    class ViewChildHolder {
        TextView group_name, group_count;
        CheckBox checkBox;
    }
}
